package com.stu.stusystem.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.stusystem.common.ApiException;
import com.stu.stusystem.common.OffsetPage;
import com.stu.stusystem.mapper.system.MenuMapper;
import com.stu.stusystem.model.system.Menu;
import com.stu.stusystem.model.system.StuUser;
import com.stu.stusystem.service.system.dto.MenuDTO;
import com.stu.stusystem.service.system.vo.MenuVO;
import com.stu.stusystem.util.CopyBeanUtil;
import com.stu.stusystem.util.TreeModelUtil;
import com.stu.stusystem.util.UUIDUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.Date;
import java.util.List;

/**
 * @author cxt
 * @date 2021/3/29
 */
@Service
public class MenuService {

    private MenuMapper menuMapper;

    /**
     * 分页查询
     */
    public PageInfo<MenuVO> getMenuPage(OffsetPage query, String name) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<MenuVO> menuVOS = this.menuMapper.getMenuPage(name);
        return new PageInfo<>(menuVOS);
    }

    /**
     * 更新菜单
     */
    public void updateMenu(MenuDTO dto) {
        Menu menu = this.menuMapper.selectByPrimaryKey(dto.getId());
        if (menu == null) {
            throw new ApiException("没有找到更新资源");
        }
        CopyBeanUtil.copyIgnore(dto, menu);
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        menu.setUpdateBy(user.getName());
        menu.setUpdateTime(new Date());
        int i = this.menuMapper.updateByExample(menu, new Example.Builder(Menu.class)
                .where(WeekendSqls.<Menu>custom().andEqualTo(Menu::getId, dto.getId())).build());
        if (i != 1) {
            throw new ApiException(ApiException.UPDATE_FAIL);
        }
    }

    /**
     * 删除菜单
     */
    public void deleteMenu(String id) {
        int i = this.menuMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            throw new ApiException(ApiException.DELETE_FAIL);
        }
    }

    /**
     * 新增菜单
     */
    public void saveMenu(MenuDTO dto) {
        List<Menu> menus = this.menuMapper.selectByExample(new Example.Builder(Menu.class)
                .where(WeekendSqls.<Menu>custom().andEqualTo(Menu::getCode,dto.getCode()).andEqualTo(Menu::getName, dto.getName())).build());
        if (menus.size() != 0) {
            throw new ApiException("您添加的菜单已存在");
        }
        Menu menu = new Menu();
        CopyBeanUtil.copyIgnore(dto, menu);
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        menu.setId(UUIDUtil.generate());
        menu.setCreateBy(user.getName());
        menu.setCreateTime(new Date());
        this.menuMapper.insert(menu);
    }

    /**
     * 查询树形菜单
     */
    public List<Object> getTreeMenu() {
        List<MenuVO> treeMenu = this.menuMapper.getTreeMenu();
        return TreeModelUtil.buildTreeList(treeMenu,"pid");
    }

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }
}
